<?php
class User
{
	/* This class will interface directly with the MySQL database, and provide methods for
	 * modifying a user's profile.
	 */

	private $UID,
			$joinDate,
			$displayName,
			$gender,
			$feedLoc,
			$twitterU,
			$twitterP,
			$fbU,
			$fbP;

	public function __construct($register=false,$args=false)
	{
		//if register=true, make new user using extra info in $args
		//else initialise variables with values in database
		if($register==true)
		{
			$this->UID = $args['uid'];
			$this->joinDate = time();
			$this->displayName = $args['displayname'];
			$this->gender = $args['gender'];
			$this->feedLoc = $args['feedLoc'];
			$this->twitterU = $args['twitteru'];
			$this->twitterP = $args['twitterp'];
			$this->fbU = $args['fbu'];
			$this->fbP = $args['fbp'];			
			$sql = "INSERT INTO users (username, password, uid, joindate, displayname, gender, feedloc, twitteru, twitterp, fbu, fbp) VALUES(" . $this->username . ", "  . $this->password . ", " . $this->UID . ", " . $this->joinDate . ", " . $this->displayName . ")";
			mysql_query($sql);
		}
		else
		{
			// Query to count number of users with this combination
      		$sql = "SELECT COUNT(*) AS num_users FROM users WHERE username=" . $this->username . " AND password=" . $this->password;
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result, MYSQL_ASSOC));
			
			// If Username/Password combination is not in DB (or duplicate user)
    		if ($row['num_users'] != 1)
    		{    
        		echo("Sorry, your username and/or password were incorrect");
    		}
       		// Else is a valid user.
   			else
    		{
				$this->username = $row['username'];
				$this->gender = $row['gender'];	
				return true;
    		}
		}
		

	}
	public function getFeed()
	{
		$feed = new Feed($this->UID);
		return $feed;
	}
	public function getDisplayName()
	{
		return $this->username;
	}
	public function setDisplayName($newName)
	{
		$this->displayName = $newName;
	}
	public function getGender()
	{
		return ($this->gender==1) ? 'Male' : 'Female';
	}
	public function setGender($newGender)
	{
		$this->gender = $newGender;
	}
	public function getFeedLocation()
	{
		return $feedLoc;
	}
	public function setFeedLocation($newLoc)
	{
		$this->feedLoc = $newLoc;
	}
	public function linkTwitter($twitterU,$twitterP)
	{
		//try to make a link with twitter, return true/false
	}
	public function linkFacebook($fbU,$fbP)
	{
		//try to make a link with fb, return true/false
	}
	public function unlinkTwitter()
	{
		//remove twitter link
	}
	public function unlinkFacebook()
	{
		//remove fb link
	}
}
?>